﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Collections.Specialized;

using Kooboo.CMS.Content.Models;
using Kooboo.CMS.Content.Query;

namespace KB
{
    public static class TextContentExtensions
    {
        #region MapTo

        public static TEntity MapTo<TEntity>(this TextContent textContent)
        {
            if (textContent != null)
            {
                return (TEntity)Activator.CreateInstance(typeof(TEntity), textContent);
            }
            return default(TEntity);
        }

        public static IEnumerable<TEntity> MapTo<TEntity>(this IEnumerable<TextContent> textContents)
        {
            return textContents.Select(it => MapTo<TEntity>(it));
        }

        #endregion

        #region Query

        public static IContentQuery<TextContent> WhereCategory(this IContentQuery<TextContent> textContents, string categoryFolderName, string categoryUUID)
        {
            var categoryFolder = new TextFolder(Repository.Current, categoryFolderName);
            var categoryQuery = categoryFolder.CreateQuery().WhereEquals(KEY.TextContents.UUID, categoryUUID);
            return textContents.WhereCategory(categoryQuery);
        }

        public static IContentQuery<TextContent> Categories(this TextContent textContent, string categoryFolderName)
        {
            var categoryFolder = new TextFolder(Repository.Current, categoryFolderName);
            return textContent.Categories(categoryFolder);
        }

        public static IContentQuery<TextContent> Published(this IContentQuery<TextContent> textContents)
        {
            return textContents.WhereEquals(KEY.TextContents.Published, true);
        }

        public static IContentQuery<TextContent> NotPublished(this IContentQuery<TextContent> textContents)
        {
            return textContents.WhereEquals(KEY.TextContents.Published, false);
        }

        #endregion

        #region Order by

        public static IContentQuery<TextContent> OrderBy(this IContentQuery<TextContent> textContents, TextFolder folder)
        {
            if (folder.OrderSetting.Direction == OrderDirection.Ascending)
            {
                return textContents.OrderBy(folder.OrderSetting.FieldName);
            }
            else
            {
                return textContents.OrderByDescending(folder.OrderSetting.FieldName);
            }
        }

        #endregion

        #region Convert

        public static NameValueCollection ToNameValueCollection(this TextContent textContent)
        {
            var collection = new NameValueCollection();
            foreach (var item in textContent)
            {
                if (item.Value != null)
                {
                    collection.Add(item.Key, item.Value.ToString());
                }
                else
                {
                    collection.Add(item.Key, string.Empty);
                }
            }

            return collection;
        }

        #endregion

        public static void ExportToExcel(this IEnumerable<TextContent> contents, IDictionary<string, string> fieldsAndHeaders, string fileName = null)
        {
            ExcelUtility.Response(ExcelUtility.ExcelRows(fieldsAndHeaders, contents), fileName);
        }
    }
}